use std::{collections::HashMap, fs::File, io::BufRead};

pub fn converter(input: &str, tp: &str) -> String {
    let file = File::open("中文繁简体字符对照表.txt").unwrap();
    let reader = std::io::BufReader::new(file);
    let mut table = HashMap::new();
    match tp {
        "t2s" => {
            for line in reader.lines() {
                let line = line.unwrap();
                let strs = line.split('(').collect::<Vec<&str>>();
                let t_char = strs[0].chars().next().unwrap();
                let s_char = strs[1].chars().next().unwrap();
                table.insert(t_char, s_char);
            }
        }
        "s2t" => {
            for line in reader.lines() {
                let line = line.unwrap();
                let strs = line.split('(').collect::<Vec<&str>>();
                let t_char = strs[0].chars().next().unwrap();
                let s_char = strs[1].chars().next().unwrap();
                table.insert(s_char, t_char);
            }
        }
        _ => {}
    }
    input
        .chars()
        .into_iter()
        .map(|ch| {
            if let Some(s_char) = table.get(&ch) {
                *s_char
            } else {
                ch
            }
        })
        .collect()
}
